﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Konyvkolcsonzo
{
    public partial class FmFelhasznalok : FormKK
    {
        List<felhasznalo> nevsor;
        Felhasznalok k;
        public FmFelhasznalok()
        {
            InitializeComponent();
            nevsor = new List<felhasznalo>();
            k = new Felhasznalok();
            this.pnlLeft.Controls.Add(label1);
            this.pnlLeft.Controls.Add(txbFelhasznalok);
            this.pnlLeft.Controls.Add(lvwFelhasznalok);
            this.pnlLeft.Controls.Add(label2);
            this.pnlLeft.Controls.Add(btnAdd);
            this.pnlLeft.Controls.Add(btnEdit);
            this.pnlLeft.Controls.Add(btnDelete);
            this.pnlLeft.Controls.Add(btnClose);
            this.pnlLeft.Controls.Add(btnExit);
            this.pnlRight.Controls.Add(lblNev);
            this.pnlRight.Controls.Add(lblTelefon);
            this.pnlRight.Controls.Add(lblEmail);
            this.pnlRight.Controls.Add(lblCsaladtag);
            this.pnlRight.Controls.Add(txbNev);
            this.pnlRight.Controls.Add(txbEmail);
            this.pnlRight.Controls.Add(txbTelefon);
            this.pnlRight.Controls.Add(chbCsaladtag);
            this.pnlRight.Controls.Add(btnOk);
            this.pnlRight.Controls.Add(btnCancel);
        }

        private void FmFelhasznalok_Load(object sender, EventArgs e)
        {
            Populate();
        }


        private void txbFelhasznalok_TextChanged(object sender, EventArgs e)
        {
            Populate();
        }
        private void Populate()
        {
            lvwFelhasznalok.Items.Clear();
            nevsor = k.getFelhasznalo(txbFelhasznalok.Text);
            for (int i = 0; i < nevsor.Count; i++)
            {
                ListViewItem sor = new ListViewItem(Convert.ToString(nevsor[i].id));
                sor.SubItems.Add(Convert.ToString(nevsor[i].nev));
                sor.SubItems.Add(Convert.ToString(nevsor[i].tel));
                sor.SubItems.Add(Convert.ToString(nevsor[i].email));
                sor.SubItems.Add(Convert.ToString(nevsor[i].csalad));
                lvwFelhasznalok.Items.Add(sor);
            }

            if (lvwFelhasznalok.SelectedItems.Count == 0 && lvwFelhasznalok.Items.Count > 0)
            {
                lvwFelhasznalok.Items[0].Selected = true;
            }
            lvwFelhasznalok.Focus();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.cmdAdd();
            PopulatePanelRigth();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (this.cmdEdit(lvwFelhasznalok.SelectedItems.Count))
            {
                PopulatePanelRight(Convert.ToInt64(lvwFelhasznalok.SelectedItems[0].SubItems[0].Text));
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.cmdOK();
            felhasznalo uj = new felhasznalo();
            uj.nev = Convert.ToString(txbNev.Text);
            uj.tel = Convert.ToString(txbTelefon.Text);
            uj.email = Convert.ToString(txbEmail.Text);
            uj.csaladtag = chbCsaladtag.Checked;
            if (this.editType())
            {
                uj.id = Convert.ToInt64(lvwFelhasznalok.SelectedItems[0].SubItems[0].Text);
                k.updateFelhasznalok(uj);
            }
            else
            {
                k.insertFelhasznalo(uj);
            }
            Populate();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.cmdCancel();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.cmdExit();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.cmdClose();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.cmdDelete(lvwFelhasznalok.SelectedItems.Count))
            {
                k.deleteFelhasznalok(Convert.ToInt64(lvwFelhasznalok.SelectedItems[0].SubItems[0].Text));
                Populate();
            }
        }

        private void PopulatePanelRight(long id)
        {
            felhasznalo f = new felhasznalo();
            f = k.getFelhasznalo(id);

            txbNev.Text = f.nev;
            txbTelefon.Text = f.tel;
            txbEmail.Text = f.email;
            chbCsaladtag.Checked = f.csaladtag;

        }

        private void PopulatePanelRigth() {
            txbNev.Text = "";
            txbTelefon.Text = "";
            txbEmail.Text = "";
            chbCsaladtag.Checked = false;
        }

    }
}
